﻿using dz.u9.jky.schema.Common;
using System;
using System.Collections.Generic;
using System.Data;
using UFIDA.U9.CBO.Pub.Controller;
using UFSoft.UBF.AopFrame.Lock;
using UFSoft.UBF.Util.DataAccess;

namespace dz.inner.jky.api
{
    public class InnerUtil
    {
        private LogUtils logUtils = new LogUtils();
        protected string entCode, userCode, org;



        private void GetApiParm()
        {
            //IDataReader dataReader;

            //string sql = $@"select EntCode,UserCode,Org from dz_api_set";

            //DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out dataReader);

            //while (dataReader.Read())
            //{
            //    entCode = dataReader.GetString(0);
            //    userCode = dataReader.GetString(1);
            //    org = dataReader.GetString(2);
            //}

            //dataReader.Close();

            entCode = "10";
            userCode ="admin";
            org = "10"; 
        }

        /// <summary>
        /// 用于动态调用U9方法，返回统一返回值，多用于新增，若成功，则返回单号/ID，若不成功则会返回失败原因
        /// </summary>
        /// <typeparam name="T">参数类型</typeparam>
        /// <param name="process">方法委托</param>
        /// <param name="parm">参数</param>
        /// <param name="specOrg">特定账套执行</param>
        /// <returns></returns>
        public U9CResult U9Invoke<T>(Func<T, U9CResult> process, T parm,string specOrg = "")
        {
            U9CResult result = new U9CResult();

            using (BPForEngine bp = new BPForEngine())
            {
                GetApiParm();
                ContextDTO cdto = new ContextDTO();
                cdto.EntCode = entCode;
                cdto.OrgCode = (specOrg == string.Empty) ? org : specOrg;
                cdto.UserCode = userCode;

                cdto.WriteToContext();

                try
                {
                    result = process(parm);
                }
                catch (Exception ex)
                {
                    logUtils.Error(ex);
                    result.IsSuccess = false;
                    result.Message = ex.Message;
                }
            }
                

            return result;
        }
        /// <summary>
        /// 多用于提交/审核
        /// </summary>
        /// <typeparam name="T">方法参数类型</typeparam>
        /// <param name="process">方法</param>
        /// <param name="parm">方法参数</param>
        /// <param name="result">传入的返回值</param>
        /// <returns></returns>
        public U9CResult U9Invoke<T>(Action<T, U9CResult> process, T parm, U9CResult result)
        {            
            try
            {
                process(parm,result);

                result.IsSuccess = true;
            }
            catch (Exception ex)
            {
                logUtils.Error(ex);
                result.IsSuccess = false;
                result.Message = ex.Message;
            }

            return result;
        }

        

        public U9CResult U9Invoke<T,P>(Func<T, U9CResult> process, List<Action<P>> actions,Func<U9CResult, P> convert, T parm)
        {
            U9CResult result = new U9CResult();
            try
            {
                result = process(parm);

                if (!result.IsSuccess)
                    return result;

                P parm1 = convert(result);

                foreach (var action in actions)
                {
                    action(parm1); // 调用每个函数
                }

                result.IsSuccess = true;

            }
            catch (Exception ex)
            {

                logUtils.Error(ex);
                result.IsSuccess = false;
                result.Message = ex.Message;
                
            }

            return result;
        }
        public U9CResult U9Invoke<T>(Action<T> process, T parm, U9CResult result)
        {
            try
            {
                process(parm);

                result.IsSuccess = true;
            }
            catch (Exception ex)
            {
                logUtils.Error(ex);
                result.IsSuccess = false;
                result.Message = ex.Message;
            }

            return result;
        }

        public U9CResult U9Invoke(List<Action> functions, U9CResult result)
        {
            try
            {
                foreach (var func in functions)
                {
                    func(); // 调用每个函数
                }

                result.IsSuccess = true;
            }
            catch (Exception ex)
            {
                logUtils.Error(ex);
                result.IsSuccess = false;
                result.Message = ex.Message;
            }

            return result;
        }

        public List<long> GetOrgs(string org)
        {
            List<long> ids = new List<long>();
            IDataReader dataReader;

            
            string sql = (org == string.Empty) ? $@"select id,code from Base_Organization where CreatedBy <>'EA'"
                : $@"select id,code from Base_Organization where CreatedBy <>'EA' and code = '{org}'";

            DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out dataReader);

            while (dataReader.Read())
            {
                long id = dataReader.GetInt64(0);
                ids.Add(id);
            }

            dataReader.Close();
            return ids;
        }

        public List<string> GetOrgCodes(string org)
        {
            List<string> orgs = new List<string>();
            IDataReader dataReader;


            string sql = (org == string.Empty) ? $@"select code from Base_Organization where CreatedBy <>'EA'"
                : $@"select code from Base_Organization where CreatedBy <>'EA' and code = '{org}'";

            DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out dataReader);

            while (dataReader.Read())
            {                
                orgs.Add(dataReader.GetString(0));
            }

            dataReader.Close();

            return orgs;
        }
    }
}
